#include "widget.h"
#include "ui_widget.h"
#include <fstream>
#include <string>
#include <QShortcut>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->comboBox->addItem("巨无霸");
    ui->comboBox->addItem("脆皮鸡腿堡");
    ui->comboBox->addItem("虾仁汉堡");
    ui->comboBox_2->addItem("薯片");
    ui->comboBox_2->addItem("鸡块");
    ui->comboBox_2->addItem("蛋挞");
    ui->comboBox_3->addItem("可乐");
    ui->comboBox_3->addItem("雪碧");
    ui->comboBox_3->addItem("果汁");
    ui->spinBox->setMinimum(1);
    ui->spinBox_2->setMinimum(1);
    ui->spinBox_3->setMinimum(1);
    ui->spinBox->setSuffix("份");
    ui->spinBox_2->setSuffix("份");
    ui->spinBox_3->setSuffix("杯");
    ui->comboBox_4->setEditable(true);
    std::ifstream file("E:/git/qt/ComboBox/config.txt");
    if(!file.is_open()) {
        qDebug() << "file open error";
    }
    std::string line;
    while(std::getline(file, line))
        ui->comboBox_4->addItem(QString::fromStdString(line));
    file.close();
    ui->horizontalSlider->setMinimum(100);
    ui->horizontalSlider->setMaximum(2000);
    ui->horizontalSlider->setSingleStep(50);
    ui->horizontalSlider->setValue(800);
    ui->verticalSlider->setMinimum(100);
    ui->verticalSlider->setMaximum(2000);
    ui->verticalSlider->setSingleStep(50);
    ui->verticalSlider->setValue(600);
    QShortcut* shortcut1 = new QShortcut(this);
    shortcut1->setKey(QKeySequence('+'));
    QShortcut* shortcut2 = new QShortcut(this);
    shortcut2->setKey(QKeySequence('-'));

    connect(shortcut1, &QShortcut::activated, this, &Widget::handlerAdd);
    connect(shortcut2, &QShortcut::activated, this, &Widget::handlerSub);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::handlerAdd()
{
    ui->horizontalSlider->setValue(ui->horizontalSlider->value() + 5);
    ui->label_currHonrizontalSlider->setText("当前值为：" + QString::number(ui->horizontalSlider->value()));
}

void Widget::handlerSub()
{
    ui->horizontalSlider->setValue(ui->horizontalSlider->value() - 5);
    ui->label_currHonrizontalSlider->setText("当前值为：" + QString::number(ui->horizontalSlider->value()));
}

void Widget::on_pushButton_clicked()
{
    qDebug() << "给" << ui->comboBox_4->currentText() << "吃"
             << ui->comboBox->currentText() << ui->spinBox->text()
             << ", " << ui->comboBox_2->currentText() << ui->spinBox_2->text()
             << ", " << ui->comboBox_3->currentText() << ui->spinBox_3->text();
}


void Widget::on_pushButton_2_clicked()
{
    QDateTime oldTime = ui->dateTimeEdit->dateTime();
    QDateTime currTime = ui->dateTimeEdit_2->dateTime();
    qDebug() << oldTime;
    qDebug() << currTime;

    int seconds = oldTime.secsTo(currTime);
    int hours = (seconds / 3600) % 24;
    int minutes = (seconds / 60) % 60;
    int days = seconds / (3600 * 24);
    qDebug() << "我已经爱你" << days << "天" << hours << "小时" << minutes << "分钟";
}


void Widget::on_dial_valueChanged(int value)
{
    qDebug() << value;
    this->setWindowOpacity(value / 100.0);
}


void Widget::on_horizontalSlider_valueChanged(int value)
{
    const QRect& rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), value, rect.height());
}

void Widget::on_verticalSlider_valueChanged(int value)
{
    const QRect& rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), rect.width(), value);
}

